# -*- coding: UTF-8 -*-
import subprocess
from config.logging_config import logger
from config.browser_settings import BrowserConfig


class BrowserHelper(object):
    def __init__(self):
        pass

    def open_browser(self,browser_config: BrowserConfig, url: str):
        logger.info("开始启动浏览器:{}", url)
        browser_address = browser_config.browser_address
        port = browser_config.port
        private_mode_arg = browser_config.private_mode_arg
        log_address = browser_config.log_address

        try:
            # 打开指定的浏览器并使用隐私模式，指定端口，最大化窗口，设置日志目录
             print("")
             subprocess.Popen([browser_address, private_mode_arg, f'--remote-debugging-port={port}', '--start-maximized', f'--log-file={log_address}'] + ([url] if url else []))
        except subprocess.CalledProcessError as e:
            logger.error("浏览器打开失败:\n{}",e)


